package com.yiqi.liebang.feature.common.view;

import android.content.Intent;
import android.support.annotation.IdRes;
import android.view.KeyEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.T;
import com.yiqi.liebang.R;
import com.yiqi.liebang.feature.home.view.AskActivity;
import com.yiqi.liebang.feature.home.view.HomeFragment;
import com.yiqi.liebang.feature.login.view.LoginActivity;
import com.yiqi.liebang.feature.mine.view.MineFragment;
import com.yiqi.liebang.feature.news.view.NewsFragment;
import com.yiqi.liebang.feature.people.view.PeopleFragment;
import com.yiqi.liebang.framework.api.UserApi;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.rong.imkit.RongIM;
import io.rong.imkit.manager.IUnReadMessageObserver;
import io.rong.imlib.model.Conversation;


public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {


    @BindView(R.id.radiogroup)
    RadioGroup mRadiogroup;

    @BindView(R.id.rb_home_lottery)
    RadioButton mRbHomeLottery;
    @BindView(R.id.rb_home_documentary)
    RadioButton mRbHomeDocumentary;
    @BindView(R.id.rb_home_find)
    RadioButton mRbHomeFind;
    @BindView(R.id.rb_home_mine)
    RadioButton mRbHomeMine;
    private HomeFragment mHomeFragment;
    public NewsFragment mNewsFragment;
    private PeopleFragment mPeopleFragment;
    private MineFragment mMineFragment;
    int num=0;
    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_main;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initView() {
        mRbHomeLottery.setText("首页");
        mRbHomeDocumentary.setText("消息");
        mRbHomeFind.setText("人脉");
        mRbHomeMine.setText("我的");


        mHomeFragment = new HomeFragment();
        mNewsFragment = new NewsFragment();
        mPeopleFragment = new PeopleFragment();
        mMineFragment = new MineFragment();
        addFragment(R.id.fl_container, mHomeFragment);
    }

    @Override
    protected void initData() {
//        AM.appUpdate().checkUpdate(this);

   getUnRead();

    }
    private void getUnRead() {
        AM.api().createApiService(UserApi.class)
                .getUserTreatedNum()
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(String s) {
                        num = Integer.valueOf(s);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    @Override
    protected  void initEvent() {
        mRadiogroup.setOnCheckedChangeListener(this);
        final Conversation.ConversationType[] conversationTypes = {
                Conversation.ConversationType.PRIVATE, Conversation.ConversationType.DISCUSSION,

                Conversation.ConversationType.GROUP, Conversation.ConversationType.SYSTEM,

                Conversation.ConversationType.PUBLIC_SERVICE, Conversation.ConversationType.APP_PUBLIC_SERVICE};


        RongIM.getInstance().addUnReadMessageCountChangedObserver(new IUnReadMessageObserver() {
            @Override
            public void onCountChanged(int i) {
                if (i > 0||num>0) {
                    mRbHomeDocumentary.setCompoundDrawablesWithIntrinsicBounds(null, getResources()
                            .getDrawable(R.drawable.bg_mesaage_unread), null, null);
                    mRbHomeDocumentary.setCompoundDrawablePadding(10);
                } else {
                    mRbHomeDocumentary.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.home_menu_news), null, null);
                    mRbHomeDocumentary.setCompoundDrawablePadding(10);
                }
            }
        }, conversationTypes);
    }



    @Override
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {

        switch (i) {

            case R.id.rb_home_lottery:
                replaceFragment(R.id.fl_container, mHomeFragment);
                break;
            case R.id.rb_home_documentary:
                if (!AM.user().isLogin()){
                    startActivity(new Intent(this, LoginActivity.class));
                    return;
                }
                replaceFragment(R.id.fl_container, mNewsFragment);
                break;
            case R.id.rb_home_find:
                if (!AM.user().isLogin()){
                    startActivity(new Intent(this, LoginActivity.class));
                    return;
                }
                replaceFragment(R.id.fl_container, mPeopleFragment);
                break;
            case R.id.rb_home_mine:
                if (!AM.user().isLogin()){
                    startActivity(new Intent(this, LoginActivity.class));
                    return;
                }
                replaceFragment(R.id.fl_container, mMineFragment);
                break;
        }
    }


    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }

    //退出时的时间
    private long mExitTime;

    //对返回键进行监听
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {

            exit();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void exit() {
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            T.showShort("再按一次退出猎帮！");
            mExitTime = System.currentTimeMillis();
        } else {
            AM.appManager().exitApp();
        }
    }

    @OnClick(R.id.btn_home_wode)
    public void onViewClicked() {
        if (!AM.user().isLogin()){
            startActivity(new Intent(this, LoginActivity.class));
            return;
        }
        startActivity(new Intent(this, AskActivity.class));
    }


//    @Override
//    protected void initSystemParams() {
////        super.initSystemParams();
//        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//        fullScreen(this);
//    }


}
